<template>
  <div>
    <div style="float:left;width:50%">
      <el-row :gutter="20" style="margin:0px" type="flex" justify="left">
        <el-col :span="2">
          <el-button size="mini" type="primary" @click="dialogVisible = true">新增</el-button>
        </el-col>
        <el-col :span="2">
          <el-button size="mini" type="primary" @click="dialogVisible = true">删除</el-button>
        </el-col>
      </el-row>

      <el-dialog
        title="新增人员"
        :visible.sync="dialogVisible"
        width="720px"
        :before-close="handleClose"
      >
        <el-form ref="form" :model="formData" label-width="80px" label-position>
          <el-form-item label="部门名称" width="100px" prop="Name">
            <el-input v-model="formData.Name"></el-input>
          </el-form-item>
          <el-form-item label="部门缩写" width="100px" prop="Name">
            <el-input v-model="formData.NameSpell"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addOrEditDepartment">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="tableData" border style="width: 100%" size="mini">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="Name" label="部门名称"></el-table-column>
        <el-table-column prop="NameSpell" label="部门缩写"></el-table-column>
        <el-table-column prop="LeaderName" label="领导"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="EditRow(scope.$index,scope.row)" style="width:40%">编</el-button>
            <el-button size="mini" @click="DeleteRow(scope.$index,scope.row)" style="width:40%">删</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="float:left;width:50%"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        // {
        //   Name: 'XXX部门',
        //   LeaderName: '王小五'
        // }
      ],
      dialogVisible: false,
      formData: {},
      isNewMode: true
    }
  },
  methods: {
    DeleteRow(index, data) {
      this.axios
        .delete('/User/DeleteDepartment', {
          data: { Guids: [data.Id] }
        })
        .then(e => {
          this.tableData.splice(index, 1)
        })
    },
    EditRow(index, data) {
      this.isNewMode = false
      this.formData = data
      this.dialogVisible = true
    },
    ClearData() {
      this.formData = {}
    },
    addOrEditDepartment() {
      if (this.isNewMode) this.addNewDepartment()
      else this.updateDepartment()
      this.dialogVisible = false
    },
    handleClose() {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.ClearData()
          this.dialogVisible = false
        })
        .catch(_ => {})
    },
    updateDepartment() {
      this.axios.put('/User/UpdateDepartment', this.formData)
    },
    addNewDepartment() {
      this.axios.post('/User/CreateDepartment', this.formData).then(e => {
        this.getAllDepartment()
      })
    },
    getAllDepartment() {
      this.axios.get('/User/GetAllDepartment').then(e => {
        this.tableData = e.data
      })
    }
  },
  created() {
    this.getAllDepartment()
  }
}
</script>

<style lang="stylus" scoped>
</style>
